#region Summary 

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution
// 
// Project :JackieHan.Hibernate2.0.Data
// 
// File : CustomerDal.cs
// 
// CopyRight : Copyright © 2009-2010 JackieHan Personal, All Rights Reserved 
// 
// Summary : 客户信息数据库操作
// 
// History : 
//			2010-08-18 00:33:31	    Create 
// 

#endregion

#region Using 

using System;
using System.Collections.Generic;
using JackieHan.Hibernate2.Domain.Entities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace JackieHan.Hibernate2.Data
{
    /// <summary> 
    /// 客户信息数据库操作
    /// </summary> 
    public class CustomerDal
    {
        private ISession session;

        public CustomerDal()
        {
            this.session = NHibernateHelper.GetSession();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="customer"></param>
        public void CreateCustomer(CustomerInfo customer)
        {
            session.Save(customer);
            session.Flush(); 
        }

        public CustomerInfo GetCustomerById(int customerId)
        {
            return session.Get<CustomerInfo>(customerId);
        }

        #region HQL
        
        public IList<CustomerInfo> From()
        {
            return session.CreateQuery("from CustomerInfo").List<CustomerInfo>();
        }

        public IList<CustomerInfo> FromAlias()
        {
            return session.CreateQuery("from CustomerInfo as customer").List<CustomerInfo>();
        }

        public IList<int> Select()
        {
            return session.CreateQuery("select c.Id from CustomerInfo c").List<int>();
        }

        public IList<object[]> SelectObject()
        {
            return
                session.CreateQuery("select c.FirstName,count(c.FirstName) from CustomerInfo c group by c.FirstName").
                    List<object[]>();
        }

        public IList<object[]> AggregateFunction()
        {
            return
                session.CreateQuery("select avg(c.Id),sum(c.Id),count(c) from CustomerInfo c").List
                    <object[]>();
        }

        public IList<string> Distinct()
        {
            return session.CreateQuery("select distinct c.FirstName from CustomerInfo c").List<string>();
        }

        public IList<CustomerInfo> Where()
        {
            return session.CreateQuery("from CustomerInfo c where c.FirstName='Jackie'").List<CustomerInfo>();
        }

        #endregion

        #region Criteria Query

        public IList<CustomerInfo> CreateCriteria()
        {
            ICriteria criteria = session.CreateCriteria(typeof (CustomerInfo));
            criteria.SetMaxResults(50);
            IList<CustomerInfo> customerInfos = criteria.List<CustomerInfo>();
            return customerInfos;
        }

        public IList<CustomerInfo> Narrowing()
        {
            IList<CustomerInfo> customerInfos = session.CreateCriteria(typeof (CustomerInfo))
                .Add(Restrictions.Like("FirstName", "Jac%"))
                .Add(Restrictions.Between("LastName", "A%", "Z%"))
                .List<CustomerInfo>();
            return customerInfos;
        }

        public IList<CustomerInfo> Order()
        {
            IList<CustomerInfo> customerInfos = session.CreateCriteria(typeof (CustomerInfo))
                .Add(Restrictions.Like("FirstName", "J%"))
                .AddOrder(new Order("FirstName", false))
                .AddOrder(new Order("LastName", true))
                .List<CustomerInfo>();
            return customerInfos;
        }

        #endregion
    }
}